package com.bibrApp.difu.actvity.jiedai_manager_mvp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bibrApp.difu.actvity.jiechu_mvp.IJiechuView;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.fragment.jieru_mvp.JiedaiManagerBean;
import com.bibrApp.difu.utils.EncryptUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class JiedaiManagerPresenter extends BasePresenter<IJiedaiManagerView> {
    public JiedaiManagerPresenter(Context context, IJiedaiManagerView iView) {
        super(context, iView);
    }

    public void getDatas(int type, int pageNum, int pageSize) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("pageNumber", pageNum);
        map.put("type", type);
        RetrofitNet.getInstance()
                .getApi()
                .getTradeList(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        JSONObject jsonObject = JSON.parseObject(EncryptUtils.objectToJson(value));
                        List<JiedaiManagerBean> list = JSON.parseArray(jsonObject.getString("list"), JiedaiManagerBean.class);
                        iView.onDataResult(list, jsonObject.getBoolean("hasNextPage"));
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        iView.onDataResult(new ArrayList<>(), false);
                    }
                });
    }

}
